package homework1;

import java.io.Serializable;
import java.util.Objects;

/**
 * 学生的信息有：学号、姓名、年龄。
 */
public class StudentInformation implements Serializable {
    private int no;
    private static final long serialVersionUID = -8187948088482014069L;
    private String name;
    private int age;

    public StudentInformation(int no, String name, int age) throws NoException, AgeException {
        setNo(no);
        setName(name);
        setAge(age);
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) throws NoException {
        if (no > 0) {
            this.no = no;
        } else {
            throw new NoException("学号不合理");
        }

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException {
        if (age > 0) {
            this.age = age;
        } else {
            throw new AgeException("年龄不合理");
        }
    }

    @Override
    public String toString() {
        return "[学号：" + no +
                ", 姓名：'" + name + '\'' +
                ", 年龄" + age +
                ']';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StudentInformation that = (StudentInformation) o;
        return no == that.no && age == that.age && Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no, name, age);
    }

}
